package com.software.motorcade.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.interchangeform.dao.InterchangeForm;
import com.software.form.interchangeform.dao.InterchangeFormTransaction;
import com.software.form.packinglist.dao.PackingList;
import com.software.form.packinglist.dao.PackingListTransaction;
import com.software.form.shippingorder.dao.ShippingOrder;
import com.software.form.shippingorder.dao.ShippingOrderTransaction;

public class MotoServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		ShippingOrderTransaction sot = (ShippingOrderTransaction)CommonUtils.getBean("shippingOrderTransaction");
		String email = (String) request.getSession().getAttribute("user");
		List<ShippingOrder> orders = sot.getOrdersByEmail(email, 0);
		if(orders != null){
			List list = new ArrayList();
			for(int i = 0; i < orders.size();i++){
				JSONArray array = new JSONArray();
				array.put(orders.get(i).getId());
				array.put(orders.get(i).getCno());
				array.put(orders.get(i).getIno());
				list.add(array);
			}
			JSONObject o = new JSONObject();
			try {
				o.put("orders", list);
				CommonUtils.outputJsonData(o.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		JSONObject o = CommonUtils.readJSONStringFromRequestBody(request, response);
		try {
			String cno = o.getString("cno");
			String ino = o.getString("ino");
			PackingListTransaction plt = (PackingListTransaction)CommonUtils.getBean("packingListTransaction");
			InterchangeFormTransaction ift = (InterchangeFormTransaction)CommonUtils.getBean("interchangeFormTransaction");
			
			PackingList pl = plt.getPackingListByNo(cno);
			InterchangeForm itf = ift.getInterFormByNo(ino);
			
			JSONObject json = new JSONObject();
			json.put("pack_no", pl.getNo());
			json.put("pack_shipper", pl.getShipper());
			json.put("pack_cno", pl.getContainerNo());
			json.put("pack_ov", pl.getOceanVessel());
			json.put("pack_cargo", pl.getCargoName());
			json.put("pack_boxes", pl.getBoxes());
			json.put("pack_quantity", pl.getQuantity());
			json.put("pack_up", pl.getUnitPrice());
			json.put("pack_tp", pl.getTotalPrice());
			json.put("pack_uv", pl.getUnitVolume());
			json.put("pack_tv", pl.getTotalVolume());
			json.put("pack_tw", pl.getTotalweight());
			json.put("pack_poo", pl.getOriginPlace());
			json.put("pack_date", pl.getDate());
			
			json.put("inter_no", itf.getNo());
			json.put("inter_company", itf.getCompany());
			json.put("inter_user", itf.getUser());
			json.put("inter_dp", itf.getDeliveryPlace());
			json.put("inter_dt", itf.getDeliverTo());
			json.put("inter_rp", itf.getReturnPlace());
			json.put("inter_voy", itf.getVoyage());
			json.put("inter_cno", itf.getContainerNo());
			json.put("inter_size", itf.getSize());
			json.put("inter_optr", itf.getOptr());
			json.put("inter_seal", itf.getSealNo());
			json.put("inter_truk", itf.getTruk());
			json.put("inter_pout", itf.getPssout());
			json.put("inter_pin", itf.getPpsin());
			json.put("inter_timeout", itf.getTimeout());
			
			CommonUtils.outputJsonData(json.toString(), response);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
